﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

namespace HFController.Public
{
    public class HTMLUPFileClass
    {
        private string extensionLim = ".jpg|.gif";
        private int fileLengthLim = 1024;//单位是KB
        private string filePath = "";
        private int fileRenameMethod = 0;//文件重命名方法，0：使用原名，1：时间+三位随机数
        private string message = "";
        private static string Folder = System.Configuration.ConfigurationManager.AppSettings["UpfileFolder"].ToString();
        //private string uploadDir1 = Folder;
        private string uploadDir = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();  //上传目录:/upload/yyyy/MM;

        public bool HTMLUpLoad(HttpPostedFile htmlFile)
        {
            string originalFilePath = "";//上传文件的路径
            string originalFileName = "";//上传文件的文件名，不带扩展名
            string fileExtension = "";//上传文件的扩展名
            string fileName = "";//上传后的新文件名
            if (htmlFile.FileName.Length > 0 && htmlFile.ContentLength > 0)
            {
                originalFilePath = htmlFile.FileName;
                originalFileName = Path.GetFileNameWithoutExtension(originalFilePath);
                fileExtension = Path.GetExtension(originalFilePath);
                if (this.extensionLim.IndexOf(fileExtension.ToLower()) < 0)
                {
                    this.message = "不能上传该类型文件(" + originalFilePath + ")，您只能上传以下文件：" + this.extensionLim;
                    return false;
                }

                if (htmlFile.ContentLength > this.fileLengthLim * 1024)
                {
                    this.message = "上传的文件(" + originalFilePath + ")大于最大限制：" + this.fileLengthLim + "KB";
                    return false;
                }

                string saveDir = HttpContext.Current.Server.MapPath("~") + "\\" + Folder + "\\" + uploadDir.Trim();
                if (!Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }

                if (fileRenameMethod == 0)
                {
                    //使用原文件名，如果文件名已存在，就在文件名后加序号
                    fileName = originalFileName + fileExtension;
                    if (File.Exists(saveDir + @"\" + fileName))
                    {
                        int i = 1;
                        while (File.Exists(saveDir + @"\" + originalFileName + "_" + i.ToString() + fileExtension))
                        {
                            i++;
                        }
                        fileName = originalFileName + "_" + i.ToString() + fileExtension;
                    }
                }
                else if (fileRenameMethod == 1)
                {
                    //使用时间+随机数重命名文件
                    string strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff");//取得时间字符串
                    Random ran = new Random();
                    string strRan = Convert.ToString(ran.Next(100, 999));//生成三位随机数
                    fileName = strDateTime + strRan + fileExtension;
                }
                
                string savePath = saveDir + @"\" + fileName;
                htmlFile.SaveAs(saveDir + @"\" + fileName);
                if (this.uploadDir.Substring(0, 1) == "~")
                {
                    this.uploadDir = this.uploadDir.Substring(1, this.uploadDir.Length - 1);
                }
                this.filePath = this.uploadDir + "/" + fileName;
                return true;
            }
            else
            {
                message = "请选择上传文件";
                return false;
            }
        }

        public string ExtensionLim
        {
            get
            {
                return this.extensionLim;
            }
            set
            {
                this.extensionLim = value;
            }
        }

        public int FileLengthLim
        {
            get
            {
                return this.fileLengthLim;
            }
            set
            {
                this.fileLengthLim = value;
            }
        }

        public string FilePath
        {
            get
            {
                return this.filePath;
            }
            set
            {
                this.filePath = value;
            }
        }

        /// <summary>
        /// 上传文件重命名方法，0：使用原文件名，若已存在就在文件名后加序号，1：使用时间+随机数作文件名
        /// </summary>
        public int FileRenameMethod
        {
            get
            {
                return fileRenameMethod;
            }
            set
            {
                fileRenameMethod = value;
            }
        }

        public string Message
        {
            get
            {
                return this.message;
            }
        }

        public string UploadDir
        {
            get
            {
                return this.uploadDir;
            }
            set
            {
                this.uploadDir = value;
            }
        }
    }
}

